import { tabbarConfig } from "./common/static/api_data.js"
import "./utils/mixins.js"
import { HiddenCall } from './request/api'
const request = new HiddenCall
// 上传拨打等服务
const setRouterConfig = require("./utils/util.js")
const formatDate = require("./utils/util.js")
App({
  phonenumber: '',
  globalData: {
    unread: '',
    uuid: null,
    Authorization: '',
    globalFormIds: [],
    ...setRouterConfig,
    ...formatDate,
    isHideTabbar: false,
    // 管理全局路由dzl
    tabbarConfig: tabbarConfig,
    // 资费窗口
    isTariff: false,
    accountNumber: '',
    houseFilter: null,
    customerFilter: null,
    phoneFilter: null,
    filterData: {},
    phoneSearch: null,
    followUpType: [], // 跟进类型
    telephone: '', // 工作电话
    lockType: null, // 掌中宝状态
    swindex: null, // 所有列表tab索引
    userInfo: null // 修改密码所用的用户特征
  },
  onLaunch: function () {
    let that = this
    wx.login({
      timeout: 2000,
      success (res) {
        var wxCode = res.code
        const updateManager = wx.getUpdateManager()
        updateManager.onCheckForUpdate(function (res) {
        })
        updateManager.onUpdateReady(function () {
          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate()
        })
        updateManager.onUpdateFailed(function () {
          // 新的版本下载失败
          request.errorMessage({ code: wxCode, account: that.globalData.accountNumber }).then((res) => {
          })
        })
      }
    })
  },
  switchNumber: function (v) {
    this.app.phonenumber = v
  },
  /**
   * 保存formId
   * author dzl
   */
  dealFormIds: function (formId) {
    let formIds = this.globalData.globalFormIds;//获取全局数据中的推送码globalFormIds数组
    if (!formIds) {
      formIds = [];
    }
    let data = {
      formId: formId,
      expire: parseInt(new Date().getTime() / 1000) + 604800 //计算７天后的过期时间时间戳
    }
    formIds.push(data); //将data添加到数组的末尾
    this.globalData.globalFormIds = formIds; //保存推送码并赋值给全局变量
  },
  /**
  * 将formIds传到后端,并清空
  * author dzl
  * updater 赵美阳
  * date 2019-04-17
  */
  saveFormIds: function () {
    let formIds = this.globalData.globalFormIds;
    if (formIds.length) {
      //globalFormIds存在的情况下，将数组转换为JSON字符串
      formIds = JSON.stringify(formIds);
      this.globalData.globalFormIds = '';
    }
    //掉接口传formids
    request.save_formids(formIds, this.globalData.token, 'hide').then(res => {
    })
  }
})
